package com.pangu.config.async;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.lang.reflect.Method;
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author Linbizhao
 * @description: pangu线程池管理
 * @since 2023/11/16
 */
@Slf4j
@Configuration
public class AsyncConfiguration implements AsyncConfigurer {

    private static final Integer CORE_THREAD_COUNT = Runtime.getRuntime().availableProcessors();

    private static final Integer QUEUE_SIZE = 100;

    @Override
    public Executor getAsyncExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(CORE_THREAD_COUNT);
        executor.setMaxPoolSize(CORE_THREAD_COUNT * 2);
        executor.setQueueCapacity(QUEUE_SIZE);
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        executor.setThreadNamePrefix("PanguTaskServiceAsyncThread-");
        executor.initialize();
        return executor;
    }

    @Override
    public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
        return new PanguAsyncExceptionHandler();
    }

    @Bean("customCommonExecutor")
    public ThreadPoolTaskExecutor customCommonExecutor() {
        int core = Runtime.getRuntime().availableProcessors();
        int corePoolSize = core * 2;
        int maxPoolSize = corePoolSize * 2;
        log.info("customCommonExecutor线程池 corePoolSize : {}, maxPoolSize : {}", corePoolSize, maxPoolSize);
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(corePoolSize);
        executor.setMaxPoolSize(maxPoolSize);
        executor.setQueueCapacity(10000);
        executor.setThreadNamePrefix("customCommonExecutor-");
        executor.initialize();
        return executor;
    }

    @Bean("customPanguBusinessAsyncExecutor")
    public ThreadPoolTaskExecutor customPanguBusinessAsyncExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(CORE_THREAD_COUNT * 3 + 1);
        executor.setMaxPoolSize(CORE_THREAD_COUNT * 6);
        executor.setQueueCapacity(QUEUE_SIZE);
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        executor.setThreadNamePrefix("盘古业务异步线程池-");
        executor.initialize();
        return executor;
    }

    @Bean("panguMessageHandlerExecutor")
    public ThreadPoolTaskExecutor panguMessageHandlerExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(CORE_THREAD_COUNT * 3 + 1);
        executor.setMaxPoolSize(CORE_THREAD_COUNT * 6);
        executor.setQueueCapacity(QUEUE_SIZE);
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        executor.setThreadNamePrefix("盘古消息处理线程池-");
        executor.initialize();
        return executor;
    }

    @Bean(name = "configUpdateExecutor")
    public ThreadPoolTaskExecutor configUpdateExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(5);
        executor.setMaxPoolSize(10);
        executor.setQueueCapacity(10);
        executor.setThreadNamePrefix("configUpdateExecutor-");
        executor.initialize();
        return executor;
    }

    static class PanguAsyncExceptionHandler implements AsyncUncaughtExceptionHandler {
        @Override
        public void handleUncaughtException(Throwable ex, Method method, Object... params) {
            log.error("异步线程池执行异常，具体详情:{}", ExceptionUtils.getStackTrace(ex));
        }
    }
}
